import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.*;

public class InitGUI implements ActionListener{
	
	protected JFrame theFrame = new JFrame("CSV-File Import");
	protected JButton browse = new JButton("Browse");
	protected JButton submit = new JButton("Submit");
	protected JTextField csvField = new JTextField(25);
	protected File file;
		
	public static void main(String[] args){				
		new InitGUI().buildGUI();	
	}

	public void buildGUI(){		

		JPanel mainPanel = new JPanel();
		JPanel csvPanel = new JPanel();
		JPanel submitPanel = new JPanel();
		JPanel browsePanel = new JPanel();
		JPanel hintPanel = new JPanel();
	
		
		//Main Frame
		theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		
		theFrame.setSize(500, 250);

		//JLabel
		JLabel hint = new JLabel("Enter CSV File Location:");
		hintPanel.add(hint);

		//Text Field for File Location
		
		csvPanel.add(csvField);
		
		//Browse Button
		browse.addActionListener(this);
		browsePanel.add(browse);
				
		//Submit Button
		submit.addActionListener(this);
		submitPanel.add(submit);
		
		// Setting Panel Sizes
		mainPanel.setSize(500,250);
		submitPanel.setSize(50,50);	
		browsePanel.setSize(50,50);
		csvPanel.setSize(200,50);
		
		// Setting Border Layouts
		mainPanel.add(BorderLayout.NORTH, hintPanel);
		mainPanel.add(BorderLayout.CENTER,csvPanel);
		mainPanel.add(BorderLayout.SOUTH,browsePanel);
		mainPanel.add(BorderLayout.CENTER, submitPanel);
		
		// Setting up the Main Frame
		theFrame.getContentPane().add(mainPanel);
		theFrame.pack();
		theFrame.setVisible(true);
		
		//CANNOT ADD PATH TO TEXT BOX WTF???!!! BAD THINGS HAPPEN!!!!!!!!!!!!!!!
		//this.csvField.setText("C:\\Users\\Steve Donaldson Jr\\Documents\\cle-2010.csv");
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
		
		if(e.getSource() == submit){
		
			//// Does button Work? -- Yep!
			//System.out.println("Submit Button Clicked");
					
				CSVReader csv = null;
				
				try {
					csv = new CSVReader(file);
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			
			//Sends the File to ListGUI
			
			try {
				new ListGUI(csv);
			} catch (IOException e1) {
				e1.printStackTrace();
			} catch (WTFException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
					
			//ListGUI.readCSV(csv);		
			
			theFrame.setVisible(false);
			
		}
		else if (e.getSource() == browse){
			System.out.println("Browse Button Clicked");
			
			JFrame chooserFrame = new JFrame();
			
			JFileChooser chooser = new JFileChooser();
			int returnVal =  chooser.showOpenDialog(chooserFrame);
			
			
			if(returnVal == JFileChooser.APPROVE_OPTION){
				file = chooser.getSelectedFile();
				System.out.println("Opening " + chooser.getSelectedFile() + "this is the file name " + file);
				csvField.setText(chooser.getSelectedFile().toString());
								
				
			}
						
		}
				
	}

}
